<?php

jimport( 'joomla.application.component.model' );

/**
 * @package Joomla
 * @subpackage Accounts
 */
class AccountsModelAccounts extends JModel
{
	/**
	 * accounts data
	 *
	 * @var array
	 */
	var $_data = null;

	/**
	 * table_prefix - table prefix for all component table
	 *
	 * @var string
	 */
	var $_table_prefix = null;

	/**
	 * Constructor
	 *
	 * @since 1.5
	 */
	function __construct()
	{
		parent::__construct();
		$this->_table_prefix =	'#__';
	}


	/**
	 * Method to get a accounts data
	 *
	 * this method is called from the owner VIEW by VIEW->get('Data');
	 * - get list of all accounts for the current data page.
	 * - pagination is spec. by variables limitstart,limit.
	 * - ordering of list is build in _buildContentOrderBy
	 * @since 1.5
	 */
	function getData()
	{
		$user =& JFactory::getUser();
		$type = ($user->get('guest')) ? 'logout' : 'login';
		if($type == 'login'){
			$anum = $this->getUserAnum($user->get('id'));
		}else{
			if (!$this->checkSecurityImagesCaptcha()){
				JError::raiseWarning("999", JText::_('INVALID_CAPTCHA_CODE'));
				return false;
        	}
			$anum = JRequest::getVar('anum', 1, 'post', 'string ' );
		}
/*		if(!$anum ){
			$this->setError(JText::_( 'MISSINGINFORMATION' ));
			return false;
		}*/
		// validate anum
		JTable::addIncludePath(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_extuser'.DS.'tables');
		$table =& JTable::getInstance('ExtUser', 'Table');
		if(!$table->isValidAnum($anum)){
			$this->setError(JText::_( $table->getError()));
			return false;
		}

		$db =& JFactory::getDBO();
		$query = 'SELECT ANUM, NAME, ADDRES, ALLSUM, MONTH, YEAR, SUM FROM ' . $this->_table_prefix .
				 'accounts WHERE ANUM='.$anum;
		$db->setQuery($query, 0);
		if(!$this->_data = $db->loadObject()){
			$this->setError(JText::_( 'ZERO_OBLIGATION' ));
			return false;
		}
		if( $this->_data->ALLSUM == 0 ){
			$this->setError(JText::_( 'ZERO_OBLIGATION' ));
			return false;
		}
		return $this->_data;
	}

	function getUserAnum($user_id)
	{
		$db =& JFactory::getDBO();
		$query = 'SELECT anum FROM ' . $this->_table_prefix .
		'abonnums WHERE userid =' . $user_id;
		$db->setQuery( $query );
		$anum = $db->loadResult();
		return $anum;
	}

	function checkSecurityImagesCaptcha() {
        global $mainframe;
        //check if that user has a capctha
        $params =&		JComponentHelper::getParams( 'com_accounts' );
		$captcha = 		$params->get('useCheckSecurityImages');
        if (!$captcha) {
            return true;
        }
        $return = false;
        $securityImagesJoomlaUserTry = JRequest::getVar('securityImagesJoomlaUserTry', false, '', 'CMD');
        $mainframe->triggerEvent('onSecurityImagesCheck', array($securityImagesJoomlaUserTry, &$return));
        return $return;
	}

	function getCaptcha(){
		$params =&		JComponentHelper::getParams( 'com_accounts' );
		$captcha = 		$params->get('useCheckSecurityImages');
		return $captcha;
	}
}
?>
